package com.WFinstitute.controller;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.WFinstitute.domain.Menu;
import com.WFinstitute.domain.News;
import com.WFinstitute.domain.User;
import com.WFinstitute.service.MenuService;
import com.WFinstitute.service.NewsService;
import com.WFinstitute.service.UserService;
import com.WFinstitute.util.MenuUtil;
import com.WFinstitute.util.Page;
import com.WFinstitute.util.RandomCodeUtil;
import com.WFinstitute.util.TrimHtmlUtil;

/**
 * 
 * @author 匹夫
 * @date 2016-7-23 下午1:02:34
 * @function
 */
	@Controller
	@RequestMapping("/back")
	public class BackLoginController {
		@Resource
		private UserService  userService;
		
		@Resource
		private MenuService  menuService;
		
		@Resource
		private NewsService  newsService;

		/**
		 * 【后台页面】 到登录页面
		 * @param response
		 * @param request
		 * @return
		 */
		@RequestMapping("/login")
		public String test(HttpServletResponse response,HttpServletRequest request){
			return "/WEB-INF/backstage/login.jsp";
		}
		
		/**
		 * 【后台页面】登录判断
		 * @param response
		 * @param request
		 * @param model
		 * @return
		 */
		@RequestMapping(value="/index")
		public ModelAndView login(HttpServletResponse response,HttpServletRequest request,Model model){
			String randomImg=request.getSession().getAttribute("randomImg")!=null?request.getSession().getAttribute("randomImg").toString():null;
			String username=request.getParameter("username");
			String password=request.getParameter("password");
			String validCode=request.getParameter("validCode");
			if(validCode!=null && !(validCode.equals(randomImg))){
				System.out.println(randomImg+"==="+validCode);
				request.setAttribute("state", "1");
				request.setAttribute("Msg", "验证码输入错误!");
				return new ModelAndView("/WEB-INF/backstage/login.jsp");
			}
			if(username==null || "".equals(username.trim()) || password==null || "".equals(password.trim())){
				request.setAttribute("state", "1");
				request.setAttribute("Msg", "用户名或密码不能为空!");
				return new ModelAndView("/WEB-INF/backstage/login.jsp");
				
			}else{
				User u=userService.login(username.trim(), password);
				if(u==null){
					request.setAttribute("state", "1");
					request.setAttribute("Msg", "用户名或密码不正确!");
					return new ModelAndView("/WEB-INF/backstage/login.jsp");
				}else{
					request.getSession().setAttribute("user", u);
					request.setAttribute("state", "0");
					request.setAttribute("Msg", "登陆成功!");
					request.setAttribute("user", u);
					System.out.println("登陆成功!");
					request.getSession().setAttribute("user", u);
					//获取菜单列表
					List<Menu> menuList = menuService.findMenuList();
					String menuJson = MenuUtil.getMenuStr(menuList);
					request.setAttribute("menu", menuJson);
					return new ModelAndView("/WEB-INF/backstage/index.jsp");
				}
			}

		}
		/**
		 * 【后台页面】刷新验证码
		 * @param response
		 * @param request
		 * @return
		 */
		@RequestMapping(value="/randomImg",method=RequestMethod.GET)
		public String randomImg(HttpServletResponse response,HttpServletRequest request){
			ServletOutputStream out=null;
			RandomCodeUtil randCode = RandomCodeUtil.instance();
			request.getSession().setAttribute("randomImg",randCode.getStr());
				try {
					out=response.getOutputStream();
					ByteArrayInputStream  in=randCode.getImage();
					byte[] bytes=new byte[1024];
					int leng=0;
					while ((leng=in.read(bytes))!=-1) {
						out.write(bytes, 0, leng);
					}
				} catch (Exception e){
					e.printStackTrace();
				}finally{
					if(out!=null){
						try {
							out.flush();
							out.close();
						} catch (IOException e) {
							e.printStackTrace();
						}
					}
				}
			return null;
		}
		
		/**
		 * 【后台页面】到新闻列表
		 * @param response
		 * @param request
		 * @return
		 */
		@RequestMapping("/news")
		public String toNewsList(HttpServletResponse response,HttpServletRequest request){
			return "/WEB-INF/backstage/news/list.jsp";
		}
		
		
		/**
		 * 【后台页面】到修改密码页面
		 * @param response
		 * @param request
		 * @return
		 */
		@RequestMapping("/editpwd")
		public String editpwd(HttpServletResponse response,HttpServletRequest request){
			return "/WEB-INF/backstage/editpwd.jsp";
		}
		
		/**
		 * 【后台页面】到学者管理列表
		 * @param response
		 * @param request
		 * @return
		 */
		@RequestMapping("/scholar")
		public String toScholarList(HttpServletResponse response,HttpServletRequest request){
			return "/WEB-INF/backstage/scholar/list.jsp";
		}
		
		
		
		/**
		 * 【后台页面】到学者头像管理列表
		 * @param response
		 * @param request
		 * @return
		 */
		@RequestMapping("/scholarimg")
		public String toscholarimg(HttpServletResponse response,HttpServletRequest request){
			return "/WEB-INF/backstage/scholar/imglist.jsp";
		}
		
		/**
		 * 【后台页面】到学者文章管理列表
		 * @param response
		 * @param request
		 * @return
		 */
		@RequestMapping("/scholararticle")
		public String toscholararticle(HttpServletResponse response,HttpServletRequest request){
			return "/WEB-INF/backstage/scholararticle/list.jsp";
		}
}
